﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;

using WFL.Threading;

namespace WFL.Demo.WinForm
{
    public class TestTimingBatch1 : TimingBatch
    {

        public TestTimingBatch1(string name) : base(name)
        {

        }

        public TestTimingBatch1(string name, NameValueCollection parameters) : base(name, parameters)
        {

        }

        public TestTimingBatch1(string name, DateTime startTime, TimeSpan interval) : base(name, startTime, interval)
        {

        }

        public override void Execute(BatchInstance instance)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + Name + @"\";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += Name + ".txt";
            string text = string.Format("\r\n-----------------------------------\r\nInstance:{0} is Running at {1}"
                , instance.Id.ToString("N")
                , DateTime.Now.ToString());
            if (instance.ExecuteData != null)
            {
                text += " with data:" + instance.ExecuteData.ToString();
            }
            text += "\r\n-----------------------------------\r\n";
            File.AppendAllText(path, text);
        }

        protected override void OnInstanceStateChange(BatchInstance instance)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + Name + @"\";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += (Name + ".txt");
            string text = string.Format("\r\nCreateTime:{0}\r\nWaitTime:{1}\r\nStartTime:{2}\r\nEndTime:{3}\r\nState:{4}"
                , instance.CreateTime.ToString()
                , instance.WaitTime.ToString()
                , instance.StartTime.ToString()
                , instance.EndTime.ToString()
                , instance.State.ToString());
            if (instance.ExecuteData != null)
            {
                text += string.Format("\r\nExecuteData:{0}", instance.ExecuteData);
            }
            if (instance.CancelData != null)
            {
                text += string.Format("\r\nCancelData:{0}", instance.CancelData);
            }
            File.AppendAllText(path, text + "\r\n");
        }
    }
}
